package com.yc.cloud.sms.sevice.impl;

import com.yc.cloud.sms.request.SmsEmailParam;
import com.yc.cloud.sms.sevice.EmailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class EmailServiceImpl implements EmailService {
    @Resource
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;

    //    public EmailServiceImpl(JavaMailSender mailSender) {
//        this.mailSender = mailSender;
//    }
    @Override
    public boolean send(SmsEmailParam param) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
//        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(param.getTo());
        //邮件主题
        message.setSubject(param.getSubject());
        //邮件内容
        message.setText(param.getContent());
        //发送邮件
        try {
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
